<<<<<<< HEAD
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 22:02:27 2017
euler project 51
@author: zhiqiang
"""
import time
t = time.clock()

from math import sqrt
def is_prime(num):#判断一个奇数是不是质数
    is_pri = True
    if num==2:
        return True
    if num==3:
        return True
    for i in range(3,int(sqrt(num))+1,2):
        if num%i==0:
            return False
    return is_pri

from itertools import combinations
from collections import Counter
import numpy as np

def findEuler51Ans():
    num = 56995
    primes = []
    digits_09 = list(map(str,list(range(10))))
    while True:
        if num in primes or is_prime(num): #如果数字是质数
            if num not in primes:
                primes.append(num)
            digitstr = str(num)       
            arr_digit = np.array(list(digitstr))
            #只计算最后一位之前的数字当中，每个数字出现的次数
            digits_num_dict = Counter(digitstr[:-1])
            for digit,number in digits_num_dict.items():
                copy_arr_digit = arr_digit.copy()
                #获取全部等于digit的可以换的位置
                replace_site = np.where(copy_arr_digit[:-1]==digit)[0]
                for replace_num in range(number,0,-1):
                    #将所有替换数是replace_num的值的可能性用组合数提取出来
                    for replace_combin in combinations(replace_site,replace_num):
                        min_prime = num
                        after_replace_all_primes = 10
                        for digit09 in digits_09:
                            #如果要替换的位置中有第一位，而且当前替换值是0直接跳过                    
                            if 0 in replace_combin and digit09=='0':
                                after_replace_all_primes -= 1
                                if after_replace_all_primes<=7:
                                    break
                                continue
                            for i in replace_combin:#替换
                                copy_arr_digit[i] = digit09
                            a = int("".join(copy_arr_digit))
                            if a in primes or is_prime(a):
                                if a not in primes:
                                    primes.append(a)
                                if a<min_prime:
                                    min_prime = a
                            else:
                                after_replace_all_primes -= 1
                                if after_replace_all_primes<=7:
                                    break
                                continue
                        if after_replace_all_primes==8:
                            print(min_prime)
                            return min_prime
                            break
            num += 2
        else:
            num += 2
            continue

if __name__=="__main__":
    findEuler51Ans()

print(time.clock()-t)
=======
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 22:02:27 2017
euler project 51
@author: zhiqiang
"""
from euler49 import is_prime

from itertools import combinations
from collections import Counter
num = 101
primes = []
while True:
    digits = len(num)
    can_be_replace = range(digits)
    if num not in primes and not is_prime(num):
        num += 2
        continue
    else:
        if num not in primes:
            primes.append(num)
            for num_replace in range(1,digits):
                for digits_replace in combinations(can_be_replace,num_replace):
                    #将所有变化放出来，查看是不是8个
                    #要求两位以上变化时，要查验原数字在这两位上是不是相同的数字，如果不是直接continue
            
        else:
            num += 2
            continue
>>>>>>> a5af79f8707695f8f57d8dfba494c266a60947c5
